package com.lvtongka.app.dialog.adapter

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvtongka.app.common.AppUtils
import com.lvtongka.app.databinding.DialogSelectCouponqsItemBinding
import com.lvtongka.app.fragment.home.bean.TravelPassAssets
import com.lvtongka.app.R

class CouponSelectQsAdapter(private var itemSelectListener: OnItemSelectListener) :
    RecyclerView.Adapter<CouponSelectQsAdapter.CouponSelectHolder>() {
    private var mList: MutableList<TravelPassAssets> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun setList(list: List<TravelPassAssets>) {
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    open inner class CouponSelectHolder(val binding: DialogSelectCouponqsItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CouponSelectHolder {
        var binding = DialogSelectCouponqsItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CouponSelectHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: CouponSelectQsAdapter.CouponSelectHolder, position: Int) {
        val data = mList[position]
        if (TextUtils.equals(data.member_type, "1")) {
            // 旅通
            holder.binding.tvQsName.text = data.name
            val bl = AppUtils.convertFenToYuan(data.balance)
            holder.binding.tvQsBalance.text = "¥$bl"
            holder.binding.ivQsPic.setImageResource(R.mipmap.icon_lv_card)
        } else {
            holder.binding.tvQsName.text = "亲属卡（${data.name}）"
            val bl = AppUtils.convertFenToYuan(data.balance)
            holder.binding.tvQsBalance.text = "剩余¥$bl"
            holder.binding.ivQsPic.setImageResource(R.mipmap.icon_coupon_qs)
        }
        holder.binding.ivSelected.visibility = View.GONE
        if (data.isSelected) {
            holder.binding.ivSelected.visibility = View.VISIBLE
        }
        holder.binding.rlQsCard.setOnClickListener {
            itemSelectListener.onItemClick(data)
        }
    }
}

interface OnItemSelectListener {
    fun onItemClick(passAssets: TravelPassAssets)
}